<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
      label-position="left">

      <div class="title-container">
        <h3 class="title">{{ $store.state.settings.name }}管理系统</h3>
      </div>
      <!-- 显示登录界面 -->
      <div v-if="isShow == true">
        <el-form-item prop="mobile">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="mobile" v-model="loginForm.mobile" placeholder="请输入账户" name="mobile" type="text" tabindex="1"
            auto-complete="on" />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
            placeholder="请输入密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
          @click.native.prevent="handleLogin">登录</el-button>
      </div>
      <!-- 显示修改密码界面 -->
      <div v-else>
        <!-- 输入新密码 -->
        <el-form-item>
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="password" v-model="data.password" placeholder="请输入新密码" name="password" type="password"
            tabindex="1" auto-complete="on" />
        </el-form-item>
        <!-- 输入确认的新密码 -->
        <el-form-item>
          <span class="svg-container">
            <svg-icon icon-class="re_password" />
          </span>
          <el-input :key="passwordType" ref="re_password" v-model="data.re_password" :type="passwordType"
            placeholder="请确认新密码" name="re_password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <!-- 输入账户 -->
        <el-form-item>
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="mobile" v-model="data.mobile" placeholder="请输入手机号" name="mobile" type="text" tabindex="1"
            auto-complete="on" />
        </el-form-item>

        <div class="sendingSam">
          <!-- 输入验证码 -->
          <el-form-item style="width: 60%;">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input ref="smg" v-model="data.smg" placeholder="请输入验证码" name="smg" type="text" tabindex="1"
              auto-complete="on" />
          </el-form-item>
          <!-- 发送验证码 -->
          <div class="msgBtn">
            <span @click="countDown">{{ smg + ss }}</span>
          </div>
        </div>

        <!-- 登录 -->
        <el-button v-if="isShow == true" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
          @click.native.prevent="handleLogin">登录</el-button>
        <el-button v-else :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
          @click.native.prevent="handleLoginen">确认修改</el-button>
      </div>
      <div class="forgetPawss" v-if="isShow == true">
        <span @click="forgetPassword">忘记密码</span>
      </div>
      <div class="forgetPawss" v-else>
        <span @click="immediately">立即登录</span>
      </div>
      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import router from "@/router";
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请填写账户'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码至少6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        mobile: '',
        password: '',
      },
      loginRules: {
        mobile: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      isShow: true,      //显示登录界面
      data: {
        mobile: '',
        password: '',
        re_password: '',
        smg: ''
      },
      smg: '发送验证码',
      ss: '',
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        // this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    console.log(this.$store.state.user)
  },
  methods: {
    // 发送验证码
    countDown() {
      if (this.smg == '发送验证码') {
        if (!(/^1[3456789]\d{9}$/).test(this.data.mobile)) {
          this.$message.error('请填写正确的手机号');
          return
        }
        this.smg = 60
        this.ss = '秒'
        this.send(this)
        this.request('admin/smsCode/', 'POST', { mobile: this.data.mobile }).then(
          res => {
            if (res.errno == 0) {
              this.$message.success(res.errmsg);
            } else {
              this.$message.error(res.errmsg);
            }
          },
          function () {
            console.log('请求失败处理');
          }
        );
      }
    },
    send(this_) { // 发送验证码
      setTimeout(function () {
        if (this_.smg > 0) {
          this_.smg -= 1
          this_.send(this_)
        } else {
          this_.smg = '发送验证码'
          this_.ss = ''
        }
      }, 1000)
    },
    // 忘记密码
    forgetPassword() {
      this.isShow = false
    },
    // 立即登录
    immediately() {
      this.isShow = true
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 确认修改密码
    handleLoginen() {
      this.request('admin/forget/', 'POST', this.data).then(
        res => {
          if (res.errno == 0) {
            this.isShow = true
            this.$message.success(res.errmsg);
          } else {
            this.$message.error(res.errmsg);
          }
        },
        function () {
          console.log('请求失败处理');
        }
      );
    },
    // 登录
    handleLogin() {
      this.request('admin/login/', 'post', this.loginForm).then(
        res => {
          if (res.errno == 0) {
            this.$message.success(`登录成功`);
            this.$store.state.user = res.data
            this.$router.push({ path: '/' })
            localStorage.setItem('admin_token_sys', res.data.token)
            localStorage.setItem('adminUserInfoExplain_sys', res.data.explain)
            localStorage.setItem('permissionstype_sys', res.data.position)
            localStorage.setItem('admin_sys', JSON.stringify(res.data))
            // this.upload()
          } else {
            this.$message.error(res.errmsg);
          }
        },
        function () {
          console.log('请求失败处理');
        }
      );
    },

    upload() {
      this.getToken().then(res => {
        localStorage.setItem('uploadData', JSON.stringify(res.data))
        console.log(res)
        this.$store.state.upload.upimg = {
          token: res.data.token,
          key: new Date().valueOf()
        }
        console.log(res)
      },
        function () {
          console.log('请求失败处理');
        }
      )
    },
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

// 发送验证码
.sendingSam {
  display: flex;
  justify-content: space-between;

  .msgBtn {
    width: 30%;
    text-align: right;

    span {
      background: #409EFF;
      padding: 10px 10px;
      color: #fff;
      line-height: 56px;
      border-radius: 5px;
      cursor: pointer;
    }
  }
}

.forgetPawss {
  width: 100%;
  text-align: right;

  span {
    color: #999999;
    cursor: pointer;
  }
}

/* reset element-ui css */
.login-container {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  background-color: #fff;
  margin: 0;
  padding: 0;
  background-image: url("https://pili-vod.guanxikeji.com/beijingtu/img/1711611695797.png");
  background-size: cover;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      // background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: #3a8ee6;
      color: black;

      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px #fff inset !important;
      //   -webkit-text-fill-color: $cursor !important;
      // }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255);
    background: #ebeef5;
    border-radius: 5px;
    color: #454545;
    padding: 5px 0px;
  }
}


.login-form {
  background-color: #fff;
  border-radius: 20px;

}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #f8f5f5;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  display: flex;
  align-items: center;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 30px;
    // margin: 0 auto;
    margin-right: 20%;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #333;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
